package com.ff.neuropp.controller;

import com.ff.neuropp.domain.Auth;
import com.ff.neuropp.domain.Role;
import com.ff.neuropp.domain.User;
import com.ff.neuropp.service.RoleService;
import com.ff.neuropp.service.UserService;
import com.ff.neuropp.util.RequiredPermission;
import com.ff.neuropp.util.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色信息
 */
@RestController
@RequestMapping("api/role")
@Api(value = "角色信息controller", tags = {"角色信息接口"})
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @PostMapping("/update")
    @RequiredPermission("/api/role/update")
    @ApiOperation(value = "更新角色信息", notes = "更新角色信息")
    public Map<String, Object> updateRole(@Valid Role role) {

        Role bak = roleService.findById(role.getId());
        if (null == bak) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "编辑错误");
        }
        List<Auth> auths = bak.getAuths();
        role.setAuths(auths);
        roleService.saveRole(role);

        return ResultTool.genSuccessResult("修改成功");
    }

    @PostMapping("/create")
    @RequiredPermission("/api/role/create")
    @ApiOperation(value = "保存角色信息", notes = "保存角色信息")
    public Map<String, Object> saveRole(@Valid Role role) {

        roleService.saveRole(role);

        return ResultTool.genSuccessResult("添加成功");
    }

    @PostMapping("/delete")
    @RequiredPermission("/api/role/delete")
    @ApiOperation(value = "删除角色信息", notes = "删除角色信息")
    public Map<String, Object> saveHotelItem(@ApiParam(required = true, value = "角色id") @RequestParam Integer id) {
        Map<String, Object> result = new HashMap<>();
        Role role = roleService.findById(id);
        if (null == role) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "角色不存在");
        }
        if ("管理员".equals(role.getRoleName())) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "管理员角色不可以删除");
        }
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        param.put("EQ_role.id", id);
        List<User> list = userService.findList(param);
        if (null != list && list.size() > 0) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "角色正在使用中，不可以删除");
        }
        roleService.deleteById(id);

        result.put("code", 10000);
        result.put("message", "删除成功");
        return result;
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询角色信息", notes = "根据id查询角色信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "序号") @RequestParam Integer id,
                                        HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Role role = roleService.findById(id);
        if (null == role) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "角色信息不存在");
        }
        result.put("role", role);
        result.put("code", 10000);
        result.put("message", "查询成功");

        return result;
    }


    @GetMapping("/list")
    @ApiOperation(value = "查询角色列表", notes = "查询角色列表")
    public Map<String, Object> findList(@ApiParam(required = false, value = "关键词") @RequestParam(required = false) String roleName,
                                        HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        if (null != roleName && roleName.length() > 0) {
            param.put("LIKE_roleName", roleName);
        }
        List<Role> list = roleService.findList(param);
        if (null != list && list.size() > 0) {
            result.put("data", list);
            result.put("total", list.size());
            result.put("message", "查询成功");
            result.put("code", 10000);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "没有查询到数据");
        }

        return result;
    }

    @GetMapping("/listpage")
    @ApiOperation(value = "查询角色分页列表", notes = "查询角色分页列表")
    public Map<String, Object> findListPage(@ApiParam(required = false, value = "角色名字 模糊搜索") @RequestParam(required = false) String roleName,
                                            @ApiParam(required = false, value = "页码 1开始") @RequestParam(required = false) Integer pageNo,
                                            @ApiParam(required = false, value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize,
                                            HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        if (null == pageNo) {
            pageNo = 1;
        } else if (pageNo < 1) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        param.put("GT_id", 0);
        if (null != roleName && roleName.length() > 0) {
            param.put("LIKE_roleName", roleName);
        }
        Page<Role> list = roleService.findPage(pageNo, pageSize, param);
        if (null != list && list.getTotalElements() > 0) {
            result.put("data", list);
            result.put("message", "分页查询成功");
            result.put("code", 10000);
        } else {
            result.put("code", 10001);
            result.put("message", "没有查询到数据");
        }

        return result;
    }

}
